<?php
/**
 *	[TWCMS] (C)2012 TongWang Inc.
 *	$Id: global_show_art.lib.php 2 2013-04-27 07:14:17Z nanown@qq.com $
 */
class global_show_art{
	function __construct($C) {
		$this->id = &$_ENV->V['id'];
		$_ENV->V['place'] = place($_ENV->V['cid']);
		$_ENV->V['topcid'] = $_ENV->V['place'][0]['cid'];
		$this->main($C);
	}

	function main($C) {
		$date = empty($C['date']) ? 'Y-m-d H:i:s' : $C['date'];
		if($_ENV->G['ishtml']) {
			$sql = 'SELECT D.*,A.*,C.dir,C.parentdir,C.show_url FROM `'.TW_DBPRE.'article` A LEFT JOIN `'.TW_DBPRE.'article_data` D ON A.id=D.id LEFT JOIN `'.TW_DBPRE.'category` C ON C.cid=A.cid WHERE A.id='.$this->id;
		}else{
			$sql = 'SELECT D.*,A.* FROM `'.TW_DBPRE.'article` A LEFT JOIN `'.TW_DBPRE.'article_data` D ON A.id=D.id WHERE A.id='.$this->id;
		}
		$_ENV->V = array_merge($_ENV->V, $_ENV->db->fetch_first($sql));
		$_ENV->V['url'] = $_ENV->G['webdir'].($GLOBALS['html_path'] = $this->set_url($_ENV->V['time'], $this->id));
		$_ENV->V['date'] = date($date, $_ENV->V['time']);

		$_ENV->G['keywords'] = $_ENV->V['title'];
		$_ENV->G['description'] = $_ENV->V['direction'] ? $_ENV->V['direction'] : $_ENV->G['webname'].', '.$_ENV->V['title'];
	}

	#前一页
	function prev() {
		$row = $_ENV->db->fetch_first('SELECT id,title,time FROM `'.TW_DBPRE.'article` WHERE cid='.$_ENV->V['cid'].' AND id<'.$this->id.' ORDER BY id DESC LIMIT 1');
		if(empty($row)) return '没有了';
		return '<a href="'.$_ENV->G['webdir'].$this->set_url($row['time'], $row['id']).'" title="'.$row['title'].'" target="_self">'.$row['title'].'</a>';
	}

	#下一页
	function next() {
		$row = $_ENV->db->fetch_first('SELECT id,title,time FROM `'.TW_DBPRE.'article` WHERE cid='.$_ENV->V['cid'].' AND id>'.$this->id.' ORDER BY id ASC LIMIT 1');
		if(empty($row)) return '没有了';
		return '<a href="'.$_ENV->G['webdir'].$this->set_url($row['time'], $row['id']).'" title="'.$row['title'].'" target="_self">'.$row['title'].'</a>';
	}

	#内页页URL处理
	function set_url($time, $id) {
		if($_ENV->G['ishtml']) {	#静态
			isset($_ENV->V['urlpreg']) || $_ENV->V['urlpreg'] = rep_url($_ENV->V['show_url'], $_ENV->V['parentdir'], $_ENV->V['dir'], $_ENV->V['cid']);
			return rep_show_url($_ENV->V['urlpreg'], $time, $id);
		}elseif($_ENV->G['isfhtml'] && $_ENV->G['custom_show']) { #内容页伪静态高级
			return (empty($_ENV->G['isfhtml']) ? 'index.php?u=' : '').$_ENV->G['url_show'].P.$id.$_ENV->G['url_show_suffix'];
		}else{ #伪静态/动态
			return (empty($_ENV->G['isfhtml']) ? 'index.php?u=' : '').$_ENV->G['url_show'].P.$id.$_ENV->G['url_show_suffix'];
		}
	}
}